package com.dashan.looperpager;

import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.LinearLayout;

import com.dashan.looperpager.adapters.LooperPagerAdapter;
import com.dashan.looperpager.view.MyViewPager;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class MainActivity extends AppCompatActivity implements MyViewPager.OnViewPagerTouchListener{

    private static final String TAG = "MainActivity";

    private static List<Integer> sPics = new ArrayList<>();

    static {
        sPics.add(R.mipmap.pic_01);
        sPics.add(R.mipmap.pic_02);
        sPics.add(R.mipmap.pic_03);
    }

    private MyViewPager mLoopPager;
    private LooperPagerAdapter mLooperPagerAdapter;
    private Handler mHandler;
    private boolean mIsTouch = false;
    private LinearLayout mPointContainer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        mHandler = new Handler();
    }

    private void initView() {
        //就是找到这个viewPager控件
        mLoopPager = this.findViewById(R.id.looper_pager);
        mPointContainer = this.findViewById(R.id.point_container);
        //设置适配器
        mLooperPagerAdapter = new LooperPagerAdapter(sPics);
        mLoopPager.setAdapter(mLooperPagerAdapter);
        //根据图片的个数，去添加点的个数
        insertPoint();
        //起始位置，是否做成动画
        mLoopPager.setCurrentItem(mLooperPagerAdapter.getDataRealSize() * 100, false);
        mLoopPager.setOnViewPagerTouchListener(this);
        //滑动监听事件
        mLoopPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                //ViewPager停下来以后选中的位置
                int realPosition;
                if (mLooperPagerAdapter.getDataRealSize() != 0) {
                    realPosition = position % mLooperPagerAdapter.getDataRealSize();
                } else {
                    realPosition = 0;
                }
                setSelectPoint(realPosition);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void setSelectPoint(int realPosition) {
        for (int i = 0; i < mPointContainer.getChildCount(); i++) {
            View point = mPointContainer.getChildAt(i);
            if (i != realPosition) {
                //普通状态
                point.setBackgroundResource(R.drawable.shape_point_normal);
            }else {
                //选中状态
                point.setBackgroundResource(R.drawable.shape_point_selected);
            }

        }
    }

    private void insertPoint() {
        for (int i = 0; i < sPics.size(); i++) {
            View point = new View(this);
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(40, 40);
            point.setBackground(getResources().getDrawable(R.drawable.shape_point_normal));
            layoutParams.leftMargin = 20;
            point.setLayoutParams(layoutParams);
            mPointContainer.addView(point);
        }
    }

    @Override
    public void onAttachedToWindow() {
        super.onAttachedToWindow();
        //当界面绑定到窗口的时候
        mHandler.post(mLooperTask);
    }

    @Override
    public void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        mHandler.removeCallbacks(mLooperTask);
    }

    private Runnable mLooperTask = new Runnable() {
        @Override
        public void run() {
            if (!mIsTouch) {
                //切换ViewPager里面的图片到下一个
                int currentItem = mLoopPager.getCurrentItem();
                mLoopPager.setCurrentItem(++currentItem, true);
            }
            mHandler.postDelayed(this, 3000);//每两秒循环执行一次本方法
        }
    };

    @Override
    public void onPagerTouch(boolean isTouch) {
        this.mIsTouch = isTouch;
    }
}